{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e4b04578",
   "metadata": {},
   "source": [
    "----------\n",
    "* 记录人：简钰英\n",
    "* 时间：2021-12-09\n",
    "* week15\n",
    "* 主要参考文档:\n",
    "> 1. [Python官方文档](https://python/org)\n",
    "> 2.[博客--函数](https://www.cnblogs.com/xuzhichao/p/11570285.html)\n",
    "\n",
    "----------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ea6c486",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "> 1. 函数的定义 ： def\n",
    "> 2. 函数名的定义规则-->变量名一样的规则\n",
    "> 3. 调用函数的方式： func_name(参数)\n",
    "> 4. 参数：\n",
    ">> 1. 形式参数（定义函数）\n",
    ">> 2. 实际参数（调用函数）\n",
    ">> 3. 有多个参数时：\n",
    ">>> 0. 关键字参数\n",
    ">>> 1. 位置参数（调用函数时）---> 关键字参数要均放在末尾（多个不考虑顺序）\n",
    ">>> 2. 默认参数（定义函数时）---> 默认参数均放在末尾（多个不考虑顺序）\n",
    "# 本周知识\n",
    "> 1. 函数的返回值\n",
    "> 2. 函数的输入参数类型\n",
    "> 3. 函数的返回参数类型\n",
    "> 4. 模块的概念\n",
    "> 5. 如何创建一个模块，并且调用自己定义的模块\n",
    "\n",
    "## 1. 函数的返回值\n",
    "> 1. 返回值关键字: return\n",
    ">> 1. 函数一定有返回值（无论是什么编程语言）\n",
    ">> 2. 在其他的编程语言中（如c语言，java等），返回值一定要写，不写会报错！；对于python（更高级的语言），返回值如果不写，给定默认值 None\n",
    ">> 3. 返回值一般为函数体运行的结果（result）（函数返回值实践1）;也可以是自定义的函数返回值(实践2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "a2447800",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数返回值实践1\n",
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + ' '+ last_name\n",
    "#     print(full_name)\n",
    "    return full_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "f84a7223",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Li Hua'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_formatted_name(\"Li\",\"Hua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "343f1236",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Li Hua\n"
     ]
    }
   ],
   "source": [
    "print(get_formatted_name(\"Li\",\"Hua\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "aba60196",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数返回值实践2\n",
    "def log_in(username,password):\n",
    "    \"\"\"验证登录信息\"\"\"\n",
    "    if username == \"Lihua\" and password == 123456:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "eeb7e997",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_in(\"Lihua\",123456)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53008174",
   "metadata": {},
   "source": [
    "## 2. 函数的输入参数类型\n",
    "> 1. 函数在调用过程中可以输入复杂的参数（例如列表、字典、集合、元组等）：\n",
    ">> 1. 函数参数为列表\n",
    ">> 2. 函数参数为字典\n",
    ">> 3. ...\n",
    "> 2. 函数可以输入不确定个数的参数：\n",
    ">> 1. *args\n",
    ">> 2. ** kwargs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "a42fc796",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_A(names):\n",
    "    \"\"\"测试函数参数输入为列表\"\"\"\n",
    "    return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "735b4182",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Lihua', 'Alex', 'Ziqi']"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_A([\"Lihua\",\"Alex\",\"Ziqi\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "9743a958",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_B(person):\n",
    "    \"\"\"测试函数参数输入为字典\"\"\"\n",
    "    return person"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "bbbd9fc3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_id': 210001, 'age': 18, 'Job': 'student'}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_B({\"stud_id\":210001,\"age\":18,\"Job\":\"student\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27394252",
   "metadata": {},
   "source": [
    "> 2. 函数可以输入不确定个数的参数：\n",
    ">> 1. *args\n",
    ">>> 1. args 可以接受多个数据（单值）\n",
    ">>> 2. args 将多个数据存在一个**元组**当中，实际还是一个变量\n",
    ">> 2. ** kwargs\n",
    ">>> 1. kwargs 可以接受多个数据(键值对)==> 关键字参数\n",
    ">>> 2. kwargs 将多个 键值对 的数据存放在一个 **字典** 中，实际上还是一个变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "5d43c954",
   "metadata": {},
   "outputs": [],
   "source": [
    "def food_ingred(food,*ingreds):\n",
    "    \"\"\"食物配料单\"\"\"\n",
    "    return food,ingreds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "12c5b40c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('pizza', ('mushroom', 'green peppers', 'extra cheese'))"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "food_ingred(\"pizza\",\"mushroom\",\"green peppers\",\"extra cheese\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "9deb6016",
   "metadata": {},
   "outputs": [],
   "source": [
    "def person(stud_name,**stud_course):\n",
    "    \"\"\"学生个人信息完善表\"\"\"\n",
    "    return stud_name,stud_course"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "309cfbd9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Lihua', {'course_星期一': 'API', 'course_星期二': 'app原型设计'})"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person(\"Lihua\",course_星期一=\"API\",course_星期二=\"app原型设计\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1642377b",
   "metadata": {},
   "source": [
    "## 3. 函数的输出（返回值）参数类型\n",
    "> 1. 在编程语言中，函数的返回值只能是一个值\n",
    "> 2. 返回值可以是 字符串、数值、列表、字典等数据（单一值）\n",
    "> 3. python可以输出多个返回值，结果会用一个元组进行封装并返回。（本质不违背函数的定义）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "bc4fd300",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test1_return():\n",
    "    \"\"\"测试返回值return为字符串\"\"\"\n",
    "    return \"你好\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "14974918",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好'"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test1_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "da8e1815",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test2_return():\n",
    "    \"\"\"测试返回值return为数值\"\"\"\n",
    "    return 12345"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "970b8885",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12345"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test2_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "2f43287e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test3_return():\n",
    "    \"\"\"测试返回值return为列表\"\"\"\n",
    "    return ['Lihua', 'Alex', 'Ziqi']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "5c486c23",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Lihua', 'Alex', 'Ziqi']"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test3_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "ddd41c9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test4_return():\n",
    "    \"\"\"测试返回值return为字典\"\"\"\n",
    "    return {'stud_id': 210001, 'age': 18, 'Job': 'student'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "d411acff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_id': 210001, 'age': 18, 'Job': 'student'}"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test4_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "fcc18ba3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ******** return 后面写多个值，将会返回一个元组（一个值，不违背函数在编程思维中的定义）\n",
    "def test5_return():\n",
    "    \"\"\"测试返回值return 为 多个值的情况\"\"\"\n",
    "    return 18,\"Lihua\",['Lihua', 'Alex', 'Ziqi'],{'stud_id': 210001, 'age': 18, 'Job': 'student'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "afb8a997",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(18,\n",
       " 'Lihua',\n",
       " ['Lihua', 'Alex', 'Ziqi'],\n",
       " {'stud_id': 210001, 'age': 18, 'Job': 'student'})"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test5_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "ae23f764",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test6_return():\n",
    "    \"\"\"测试返回值return 为 多个值的情况\"\"\"\n",
    "    return 88,89,90,90\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "fa0a25b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(88, 89, 90, 90)"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test6_return()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4522bb82",
   "metadata": {},
   "source": [
    "## 4. 模块\n",
    "> 1. 使用模块可以隐藏函数的源代码\n",
    "> 2. 使用的人不需要会底层代码（只需要会使用模块即可）\n",
    "> 3. 模块的使用的方法：\n",
    ">> 1. import 模块名\n",
    ">>> * 使用： 模块名.函数名() \n",
    ">> 2. from 模块名 import 函数名\n",
    ">>> * 使用： 函数名()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "3a36c1bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "35a6b09e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=12, tm_mday=15, tm_hour=23, tm_min=35, tm_sec=33, tm_wday=2, tm_yday=349, tm_isdst=0)"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "a2f8e129",
   "metadata": {},
   "outputs": [],
   "source": [
    "from time import localtime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "29df925e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=12, tm_mday=15, tm_hour=23, tm_min=35, tm_sec=35, tm_wday=2, tm_yday=349, tm_isdst=0)"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7c13b1f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eeb54d8b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4f8a6c1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b74fa59",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
